package com.parking.carsystem.parkingchargesystem.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.adapter.PaySettingAdapter;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;
import com.parking.carsystem.parkingchargesystem.view.SwipeRecyclerView;

import java.util.ArrayList;

import butterknife.BindView;

public class PaySettingActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.pay_setting)
    SwipeRecyclerView paySetting;
    ArrayList<GroupListModule.DataBean.VehiclesBean> vehiclesBeans ;
    private PaySettingAdapter paySettingAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_paysetting;
    }

    @Override
    public void initView() {
        back.setVisibility(View.VISIBLE);
        text.setText("支付配置");
    }

    @Override
    public void initData() {
        if (vehiclesBeans == null) {
            vehiclesBeans = new ArrayList<>();
        }
        paySetting.setLayoutManager(new LinearLayoutManager(this));
        paySettingAdapter = new PaySettingAdapter(vehiclesBeans,this);
        paySetting.setAdapter(paySettingAdapter);
        getGroupList();
    }

    /**
     * 获取分组列表
     */
    private void getGroupList() {
        showLoading();
        OkGo.<String>get(Constant.url + Constant.GROUP_LIST_DATA)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        Gson gson = new Gson();
                        GroupListModule groupListModule = gson.fromJson(response.body(), GroupListModule.class);
                        if (groupListModule.code == 200) {
                            //把所有的数据放到一个集合里面
                            for (GroupListModule.DataBean datum : groupListModule.data) {
                                GroupListModule.DataBean.VehiclesBean vehiclesBean = new GroupListModule.DataBean.VehiclesBean();
                                vehiclesBean.plateNumber = datum.groupName;
                                vehiclesBean.type = true;
                                vehiclesBeans.add(vehiclesBean);
                                vehiclesBeans.addAll(datum.vehicles);
                            }
                            paySettingAdapter.setDatas(vehiclesBeans);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                    }
                });
    }
}
